package cn.tx.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;

import cn.tx.model.Dep;
import cn.tx.model.Emp;
import cn.tx.query.EmpQuery;
import cn.tx.service.DepService;
import cn.tx.service.EmpService;
import cn.tx.util.MD5Utils;
import cn.tx.util.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class EmpAction extends BaseAction implements ModelDriven<Emp>{
	
	private EmpService empService;
	
	private DepService depService;
	
	private Emp emp = new Emp();
	
	private String captcha;
	
	private String errorTip;
	
	
	
	

	public String getErrorTip() {
		return errorTip;
	}

	public void setErrorTip(String errorTip) {
		this.errorTip = errorTip;
	}

	public String getCaptcha() {
		return captcha;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	/**
	 * 查询对象
	 */
	private EmpQuery erpQuery = new EmpQuery();
	
	public Emp getEmp() {
		return emp;
	}

	public void setEmp(Emp emp) {
		this.emp = emp;
	}
	
	public DepService getDepService() {
		return depService;
	}

	public void setDepService(DepService depService) {
		this.depService = depService;
	}
	public EmpQuery getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(EmpQuery erpQuery) {
		this.erpQuery = erpQuery;
	}

	public EmpService getEmpService() {
		return empService;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}

	
	public String toLogin(){
		return LOGIN;
	}
	
	public String emp_login(){
		//获得session中的验证码
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session2 = context.getSession();
		String piccode = (String) session2.get("piccode");
		if(!StringUtils.equalsIgnoreCase(piccode, captcha)){
			errorTip = CAP_ERROR;
			return LOGIN;
		}
		String password = MD5Utils.md5(emp.getPassword());
		emp = empService.login(emp.getUsername(), password);
		if(emp == null){
			errorTip = USER_PASS_ERROR;
			return LOGIN;
		}
		//把用户对象放入session
		session2.put("user", emp);
		return "loginsuccess";
	}
	
	public void emp_getImage() throws Exception{
		 System.out.println("#######################生成数字和字母的验证码#######################");  
	        BufferedImage img = new BufferedImage(68, 22,  
	  
	        BufferedImage.TYPE_INT_RGB);  
	  
	        // 得到该图片的绘图对象    
	  
	        Graphics g = img.getGraphics();  
	  
	        Random r = new Random();  
	  
	        Color c = new Color(200, 150, 255);  
	  
	        g.setColor(c);  
	  
	        // 填充整个图片的颜色    
	  
	        g.fillRect(0, 0, 68, 22);  
	  
	        // 向图片中输出数字和字母    
	  
	        StringBuffer sb = new StringBuffer();  
	        
	  
	        char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();  
	  
	        int index, len = ch.length;  
	  
	        for (int i = 0; i < 4; i++) {  
	  
	            index = r.nextInt(len);  
	  
	            g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt  
	  
	            (255)));  
	  
	            g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 22));  
	            // 输出的  字体和大小                      
	  
	            g.drawString("" + ch[index], (i * 15) + 3, 18);  
	            //写什么数字，在图片 的什么位置画    
	  
	            sb.append(ch[index]);  
	  
	        }  
	        //把验证码的值放入session中
	        request.getSession().setAttribute("piccode", sb.toString());  
	        //把验证码的图片写回浏览器
	        ImageIO.write(img, "JPG", response.getOutputStream());  
	}
	
	/**
	 * 查询员工的列表
	 * @return
	 */
	public String emp_list(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		Page page = empService.selectObjectByConditionPage(erpQuery, exclude);
		//查询所有部门信息
		List<Dep> dlist = depService.listAll();
	
		context.put("page", page);
		context.put("dlist", dlist);
		return SUCCESS;
	}
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String emp_input(){
		ActionContext context = ActionContext.getContext();
		List<Dep> dlist = depService.listAll();
		
		context.put("dlist", dlist);
		
		return SUCCESS;
	}
	/**
	 * 跳转到修改页面
	 * @return
	 */
	public String emp_preupdate(){
		
		return SUCCESS;
	}
	/**
	 * 保存
	 * @return
	 */
	public void emp_save(){
		emp.setPassword(MD5Utils.md5(emp.getPassword()));
		empService.save(emp);
		try {
			response.getWriter().write("yes");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 修改
	 * @return
	 */
	public String emp_upate(){
		
		return TOLIST;
	}
	/**
	 * 修改
	 * @return
	 */
	public String emp_delete(){
		
		return TOLIST;
	}
	
	/**
	 * 校验用户是否已经存在
	 */
	public void emp_validUname(){
		//成功标志
		String flag = "yes";
		emp = empService.getEmpByUsername(emp.getUsername());
		if(emp != null){
			flag = "no";
		}
		try {
			response.getWriter().write(flag);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Emp getModel() {
		return emp;
	}
	
	
	
	

}
